package com.syxp.pda.action.plan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.CHKLine;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.service.base.DepartmentService;
import com.syxp.pda.service.plan.CHKLineConfService;
import com.syxp.pda.service.plan.CHKLineService;

public class CHKLineConfAction extends BaseAction {
	@Resource
	private CHKLineService chklineService;
	
	@Resource
	private DepartmentService departmentService;
	
	@Resource
	private CHKLineConfService lineconfService;
	
	private int lineID;
	private String ParentID;
	private String ids;
	
	public String loadLineTree() {
		List list = new ArrayList(); 
		try {
			List<CHKLine> lines = this.chklineService.getDao().getAll();
			for(CHKLine o: lines) {
				Map map = new HashMap();
				map.put("id", o.getChkLineID());
				map.put("text", o.getLineName());
				map.put("leaf", true);
				list.add(map);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String loadUserTree() {
		System.out.println("parid="+this.ParentID+" roleid="+this.lineID);
		JsonArray jarray = new JsonArray() ;
		try {
			String[] temp = this.ParentID.split("#");
			if(temp.length==1||temp.length==2) {//点击厂、部门节点 获取子部门和值次
				QueryFilter filter = new QueryFilter() ;
				filter.addFilter(new FieldBean("parentID",(Object)temp[temp.length-1],"EQ")) ;
				List<Department> all = departmentService.getDao().getAll(filter) ;
				for(Department dept : all) {
					JsonObject jobj = new JsonObject() ;
					jobj.addProperty("id", temp[0]+"#"+String.valueOf(dept.getDepartmentID())) ;
					jobj.addProperty("text", dept.getDepName()) ;
					//jobj.addProperty("cls", "folder") ;
					jarray.add(jobj) ;
				}
				List<Duty> dutys = this.lineconfService.getDutys(Integer.parseInt(temp[temp.length-1]));
				for(Duty o : dutys) {
					JsonObject jobj = new JsonObject() ;
					jobj.addProperty("id", this.ParentID+"#"+String.valueOf(o.getDutyID())) ;
					jobj.addProperty("text", o.getDutyName()) ;
					//jobj.addProperty("cls", "folder") ;
					jarray.add(jobj) ;
				}
			} else if(temp.length==3) {//点击值次节点  获取值次下人员
				List<User> users = this.lineconfService.getUsers(Integer.parseInt(temp[temp.length-1]));
				for(User o : users) {
					JsonObject jobj = new JsonObject() ;
					jobj.addProperty("id", this.ParentID+"#"+String.valueOf(o.getUserID())) ;
					jobj.addProperty("text", o.getUserName()) ;
					jobj.addProperty("leaf", true);
					boolean b = false;
					Set<CHKLine> lines = o.getChkLines();
					if(lines!=null&&lines.size()>0) {
						for(CHKLine l: lines) {
							if(this.lineID==l.getChkLineID()) {
								b = true;
								break;
							}
						}
					}
					jobj.addProperty("checked", b);
					jarray.add(jobj) ;
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = gson.toJson(jarray) ;
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	
	public String setLineUser() {
		try {
			System.out.println(this.ids+this.lineID);
			this.lineconfService.updateLine(this.lineID, this.ids);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	/**
	 * @return the chklineService
	 */
	public CHKLineService getChklineService() {
		return chklineService;
	}
	/**
	 * @param chklineService the chklineService to set
	 */
	public void setChklineService(CHKLineService chklineService) {
		this.chklineService = chklineService;
	}

	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	/**
	 * @param departmentService the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * @return the lineconfService
	 */
	public CHKLineConfService getLineconfService() {
		return lineconfService;
	}

	/**
	 * @param lineconfService the lineconfService to set
	 */
	public void setLineconfService(CHKLineConfService lineconfService) {
		this.lineconfService = lineconfService;
	}

	/**
	 * @return the lineID
	 */
	public int getLineID() {
		return lineID;
	}

	/**
	 * @param lineID the lineID to set
	 */
	public void setLineID(int lineID) {
		this.lineID = lineID;
	}

	/**
	 * @return the parentID
	 */
	public String getParentID() {
		return ParentID;
	}

	/**
	 * @param parentID the parentID to set
	 */
	public void setParentID(String parentID) {
		ParentID = parentID;
	}

	/**
	 * @return the ids
	 */
	public String getIds() {
		return ids;
	}

	/**
	 * @param ids the ids to set
	 */
	public void setIds(String ids) {
		this.ids = ids;
	}

}
